package br.ucb.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.ucb.enumeradores.Sexo;
import br.ucb.modelo.negocio.User;
import br.ucb.modelo.dao.BazarDAO;
import br.ucb.modelo.dao.ProdutoDAO;
import br.ucb.modelo.dao.UserDAO;
import br.ucb.service.Servicos;
import br.ucb.service.Validacao;

@WebServlet("/controleuser")
public class ControleUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pagina = null;
		RequestDispatcher disp;
		User user = null;
		HttpSession session = null;
		String acao = request.getParameter("acao");
		if(acao.equals("cadastrar")) {
			if(!Validacao.validaDadosCadastroUser(request, new UserDAO())) {
				pagina = "cadastraUser.jsp";
				request.setAttribute("sexos", Sexo.FEMININO);
			    request.setAttribute("estados", Servicos.obtemEstados());
			} else {
				if(Servicos.cadastraUser(Servicos.montaUserRequest(request), new UserDAO())) {
					request.setAttribute("sucessoCadastro", "");
					request.setAttribute("sexos", Sexo.FEMININO);
					pagina = "login.jsp";
				} else {
					request.setAttribute("erroCadastroBanco", "");
					request.setAttribute("estados", Servicos.obtemEstados());
					request.setAttribute("sexos", Sexo.FEMININO);
					pagina = "cadastraUser.jsp";
				}
					
			}
		} else if(acao.equals("logar")) {
			if(!Validacao.validaLoginUser(request, new UserDAO()))
				pagina = "login.jsp";
			else {
				pagina = "indexSessaoUser.jsp";
				user = Servicos.recuperaUserDaoLogin(request.getParameter("login"), new UserDAO());
				session = request.getSession();
				session.setMaxInactiveInterval(600);
				session.setAttribute("usuario", user);
			}
		} else if(acao.equals("sair")) {
			pagina = "login.jsp"; 
			session = request.getSession();
	    	session.removeAttribute("usuario"); 
		} else if(acao.equals("alterar")) {
			if(!Validacao.validaAlteracao(request, user)) {
				pagina = "alterarUser.jsp";
				request.setAttribute("estados", Servicos.obtemEstados());
				request.setAttribute("sexos", Sexo.FEMININO);
			} else {
				pagina = "indexSessaoUser.jsp";
				session = request.getSession(true);
				user = Servicos.montaUserRequest(request);
				if(Servicos.alterarUser(user ,new UserDAO())) {
				  request.setAttribute("sucessoAlterar", "");
				  session.setAttribute("usuario", user);
				  session.setMaxInactiveInterval(6);
				}
			} 
		} else if(acao.equals("excluir")) {
			pagina = "login.jsp";
			session = request.getSession();
			session.setMaxInactiveInterval(0);
			user = (User) session.getAttribute("usuario");
			if(Servicos.excluiUser(request, user ,new UserDAO(), new BazarDAO(), new ProdutoDAO())) {
				request.setAttribute("sucessoExcluir", "");
			}
			else
				request.setAttribute("falhaExcluir", "");
		}
		disp = request.getRequestDispatcher(pagina);
		disp.forward(request, response);
	}

}